package com.cskaoyan.wordmemorize.controller.app;



import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.auth.SaUserCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.constant.PermissionCodeConstants;
import com.cskaoyan.wordmemorize.common.constant.SystemPermissions;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.dto.app.AIReviewReadDTO;
import com.cskaoyan.wordmemorize.dto.app.DisplayUserVocSentenceDTO;
import com.cskaoyan.wordmemorize.dto.app.UserFavoriteDTO;
import com.cskaoyan.wordmemorize.dto.app.UserVocBookStatisticsDTO;
import com.cskaoyan.wordmemorize.request.*;
import com.cskaoyan.wordmemorize.service.UserPrivilegeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Tag(name = "移动端-(用户)特权模块")
@RestController
@RequestMapping("user/priviledge")
public class UserPrivilegeController {


    @Autowired
    UserPrivilegeService userPrivilegeService;
    /**
     *  获取AI阅读风格
     * @return
     */
    //@SaUserCheckPermission("review:ai:read")
    @PostMapping ("/review/ai/read/options")
    public Result<List<UserVocAISituationDTO>> getAllOptions(){
        List<UserVocAISituationDTO> aiReviewReadOptions = userPrivilegeService.getAIReviewReadOptions();
        return Result.ok(aiReviewReadOptions);
    }

    /**
     * 获取AI阅读剩余免费使用次数
     * @return
     */
//    @SaUserCheckPermission(PermissionCodeConstants.Level.Privilege.SEARCH)
    @PostMapping ("/review/ai/read/free")
    public Result<Integer> getFreeTimes(){
        long loginIdAsLong = StpKit.USER.getLoginIdAsLong();
//        long loginIdAsLong = 1897849875485937665l;
        Integer userVocAIReadRemainFreeCount = userPrivilegeService.getUserVocAIReadRemainFreeCount(loginIdAsLong);
        return Result.ok(userVocAIReadRemainFreeCount);
    }

    /**
     * 批量取消收藏
     * @param batchDeleteCommand
     * @return
     */
    @SaUserCheckPermission("my:favorite")
    @PostMapping ("/favorite/cancel")
    public Result favoriteCancel(@RequestBody BatchDeleteCommand batchDeleteCommand){
        List<String> ids = batchDeleteCommand.getIds();
        List<Long> collect = ids.stream().map(id -> Long.valueOf(id)).collect(Collectors.toList());
        userPrivilegeService.cancelFavorite(collect,StpKit.USER.getLoginIdAsLong());
        return Result.ok();
    }

    /**
     * 批量添加收藏
     * @return
     */
    @PostMapping("/favorite/bulk")
    @SaUserCheckPermission("my:favorite")
    public Result favoriteBulk(@RequestBody List<UserFavoriteCommand> userFavoriteCommands){
        userPrivilegeService.addFavoriteBulk(userFavoriteCommands,StpKit.USER.getLoginIdAsLong());
        return null;
    }

    /**
     * 添加用户收藏单词
     * @param userFavoriteCommand
     * @return
     */
    @PostMapping("/favorite")
    @SaUserCheckPermission("my:favorite")
    public Result addFavorite(@RequestBody UserFavoriteCommand userFavoriteCommand){
        userPrivilegeService.addFavorite(userFavoriteCommand, StpKit.USER.getLoginIdAsLong());
        return null;
    }


    /**
     * 获取自建例句
     * @return
     */
    @SaUserCheckPermission("create:my:example")
    @GetMapping("voc/example/list")
    public Result getExampleList(PageRequest pageRequest){
        PageDTO<DisplayUserVocSentenceDTO> userVocSentence
                = userPrivilegeService.getUserVocSentence(pageRequest, StpKit.USER.getLoginIdAsLong());
        return Result.ok();
    }


    @GetMapping("/favorite/list")
    public Result<PageDTO<UserFavoriteDTO>> getUserFavoriteList(PageRequest request) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<UserFavoriteDTO> page = userPrivilegeService.getUserFavoriteList(request, userId);
        return Result.ok(page);
    }

    @GetMapping("/current/{bookId}/statistics")
    public Result<UserVocBookStatisticsDTO> getSelectedBookStatistics(@PathVariable("bookId") Long bookId) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserVocBookStatisticsDTO dto = userPrivilegeService.getSelectedBookStatistics(userId, bookId);
        return Result.ok(dto);
    }

    @DeleteMapping("/voc/example/{id}")
    public Result<?> deleteUserVocSentence(@PathVariable("id") Long id) {
        userPrivilegeService.deleteUserVocSentence(id);
        return Result.ok();
    }

    @PutMapping("voc/non/cancel")
    // @SaUserCheckPermission("review:not:remember")
    public Result cancelNotRemember(@RequestParam Long vocId) {
        userPrivilegeService.cancelNotRemember(vocId, StpKit.USER.getLoginIdAsLong());
        return Result.ok();
    }

    @PostMapping("voc/non")
    // @SaUserCheckPermission("review:not:remember")
    public Result notRemember(@RequestParam Long vocId) {
        userPrivilegeService.notRemember(vocId, StpKit.USER.getLoginIdAsLong());
        return Result.ok();
    }

    @PostMapping("voc/example")
    // @SaUserCheckPermission("create:my:example")
    public Result addUserVocSentence(@RequestBody UserVocExampleSentenceCommand command) {
        userPrivilegeService.addUserVocSentence(command, StpKit.USER.getLoginIdAsLong());
        return Result.ok();
    }

    @PutMapping("voc/example")
    // @SaUserCheckPermission("update:my:example")
    public Result updateUserVocSentence(@RequestBody UserVocExampleSentenceCommand command) {
        userPrivilegeService.updateUserVocSentence(command, StpKit.USER.getLoginIdAsLong());
        return Result.ok();
    }

    @PostMapping("review/ai/read")
    public Result<List<AIReviewReadDTO>> getAIReviewReadContent(@RequestBody AIReviewReadCommand command) {
        List<AIReviewReadDTO> aiReviewReadContent = userPrivilegeService.getAIReviewReadContent(command, StpKit.USER.getLoginIdAsLong());
        return Result.ok(aiReviewReadContent);
    }
}
